/*******************************************************************************
*Project :       	Covid-19 Stay At Home Paper
*Purpose : 		    Master Do-file that produces all results
*STATA version: 	17.0
*Author: 			Aastha Rajan, Diane Alexander, and Ezra Karger (karger@uchicago.edu)
*Created: 	    	12/14/2020
*Last modified:		07/30/2021
*******************************************************************************/

clear all 
set more off 
mac drop _all 
capture log close

global user ezrak // change user and cd accordingly 

if "${user}" == "g1dxa03" { 
	cd "/Users/g1dxa03/Dropbox (Penn)/stay_at_home_project/"
	global machine "/Users/g1dxa03/Dropbox (Penn)/stay_at_home_project/"
} 


else if "${user}" == "ezrak" { 
	cd "C:/Users/${user}/Dropbox/stay_at_home_project"
	global machine "C:/Users/${user}/Dropbox/stay_at_home_project/"
} 


* install necessary packages for this project


capture ssc install egenmore
capture ssc install blindschemes
capture ssc install mdesc
capture ssc install ivreg2, replace
capture ssc install reghdfe
capture ssc install ivreghdfe
capture ssc install unique
capture ssc install carryforward
capture ssc install reg2hdfe
capture ssc install TMPDIR
capture ssc install ranktest
capture ssc install estout
net install rscript, from("https://raw.githubusercontent.com/reifjulian/rscript/master") replace



* generate global date
global date=subinstr(c(current_date), " ", "_", .)
set obs 1 
gen date="${date}"
split date, parse("_")
cap replace date=date3+"_"+date2+"_"+date4 if date1==""
cap replace date=date2+"_"+date1+"_"+date3 if date1!=""
global date=date[1]
display as error "Today's date = ${date}"

*===============================================================================
* I. Preparing Datasets 
*===============================================================================

* CONSUMER SPENDING - WOMPLY & SECOND MEASURE 
* pre-run in R for Womply: convert to dta files 
clear all
do programs/1.1_clean_second_measure.do
clear all
do programs/1.2_clean_womply.do  

* COUNTY CHARACTERISTICS
clear all
do programs/1.3_clean_usda.do 

* WEATHER DATA 
clear all
do programs/1.4_clean_weather.do

* MAPS DATA - Stay at Home Order Timing and Variation 
clear all
do programs/1.5_clean_stayathomeorders.do  

* HORSE RACE DATA 
clear all
do programs/1.6_construct_horseracedata.do

*===============================================================================
* II. Figures 
*===============================================================================

*~~~~~~~~~~~~~~~~ Calendar Time Analyses ~~~~~~~~~~~~~~~~*

* COVID-19 SPREAD IN CALENDAR TIME AND HISTOGRAMS STAY-AT-HOME DATES 
clear all
do programs/2.1_calender_time.do 
cd "$machine"

*~~~~~~~~~~~~~~~~ Maps ~~~~~~~~~~~~~~~~*

* STAY AT HOME ORDER MAPS - !! Run in R 
*!R CMD BATCH programs/2.2_create_maps.R 
rscript using programs/2.2_create_maps.R

*~~~~~~~~~~~~~~~~ Aggregate Trends ~~~~~~~~~~~~~~~~*

* AGGREGATE TRENDS IN CONSUMER SPENDING
clear all
do programs/2.3_create_trend_graphs.do 



*~~~~~~~~~~~~~~~~ Event - Time Analyses ~~~~~~~~~~~~~~~~*

* EFFECT ON MOBILITY AND SMALL BUSINESS REVENUE - W/O State X Day Fixed Effects 
clear all
do programs/2.4_eventtimes_no_stateday_fes.do 
cd "$machine"

* EFFECT ON MOBILITY AND SMALL BUSINESS REVENUE - With State X Day Fixed Effects
clear all
do programs/2.5_eventtimes_with_stateday_fes.do 
cd "$machine"

* EFFECT ON LARGE BUSINESSES
clear all
do programs/2.6_event_second_measure.do 
cd "$machine"

* CALLAWAY AND SANT'ANNA EVENT STUDIES FOR WOMPLY- !! Run in R 
*!R CMD BATCH programs/2.7_did_eventstudies_womply.R 
clear all
rscript using programs/2.7_did_eventstudies_womply.R
cd "$machine"

* CALLAWAY AND SANT'ANNA EVENT STUDIES FOR SECOND MEASURE- !! Run in R 
*!R CMD BATCH programs/2.8_did_eventstudies_secondmeasure.R
clear all
rscript using programs/2.8_did_eventstudies_secondmeasure.R
cd "$machine"


*===============================================================================
* III. Tables
*===============================================================================

*~~~~~~~~~~~~~~~~ Horse Race Regression ~~~~~~~~~~~~~~~~*

* HORSE RACE REGRESSION 
clear all
do programs/3_analyze_horserace_2bins.do 


*~~~~~~~~~~~~~~~~ Summary Tables ~~~~~~~~~~~~~~~~*

* SUMMARY STATS: SECOND MEASURE 
clear all
do programs/3_summary_second_measure.do 

* SUMMARY STATS: WOMPLY
clear all
do programs/3_summary_womply.do 



*===============================================================================
* IV. Data appendix
*===============================================================================
clear all
do programs/4_data_appendix.do 



*===============================================================================
* V. Rename exhibits to match paper format
*===============================================================================
clear all
do programs/5_rename_exhibits.do 

*===============================================================================
* VI. Rename appendix exhibits to match paper format
*===============================================================================
clear all
do programs/6_rename_appendix_exhibits.do

*===============================================================================
* VII. Produce statistics to be included in paper 
*===============================================================================
clear all
do programs/7_stat_checks
